﻿/*
Copyright (c) 2008 Martin Legris

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following 
conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ignore
*/
package newcommerce.lastfm.api.clients 
{
	import flash.events.ErrorEvent;
	import newcommerce.lastws.AbstractClient;
	import com.gsolo.encryption.MD5;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import newcommerce.lastfm.api.events.LFMLibraryAddEvent;
	import newcommerce.lastfm.api.LastFMClient;
	import newcommerce.lastws.RequestWrapper;
	
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class LibraryClient extends AbstractClient
	{
		public function LibraryClient() 
		{
			super();
		}
		
		public function addAlbum(artistName:String, albumName:String):Number
		{
			var url:String = LastFMClient.baseUrl;
			var sig:String = "album"+albumName+"api_key" + LastFMClient.apiKey + "artist" + artistName + "methodlibrary.addAlbumsk" + LastFMClient.sessionKey + LastFMClient.secret;
			var request:URLRequest = new URLRequest(url);
			
			var variables:URLVariables = new URLVariables();
			variables.artist = artistName;
			variables.album = albumName			
			variables.sk = LastFMClient.sessionKey;
			variables.api_sig = MD5.encrypt(sig);
			variables.method = "library.addAlbum";
			variables.api_key = LastFMClient.apiKey;
			
			request.data = variables;
			request.method = URLRequestMethod.POST;
			
			return runLoader(request, doAlbumAdded, doAlbumAddError, createWrapper( { method:"library.addAlbum", artist:artistName, album:albumName } ));
		}
		
		public function addArtist(artistName:String):Number
		{
			var url:String = LastFMClient.baseUrl;
			var sig:String = "api_key" + LastFMClient.apiKey + "artist" + artistName + "methodlibrary.addArtistsk" + LastFMClient.sessionKey + LastFMClient.secret;
			var request:URLRequest = new URLRequest(url);
			
			var variables:URLVariables = new URLVariables();
			variables.artist = artistName;
			variables.sk = LastFMClient.sessionKey;
			variables.api_sig = MD5.encrypt(sig);
			variables.method = "library.addArtist";
			variables.api_key = LastFMClient.apiKey;
			
			request.data = variables;
			request.method = URLRequestMethod.POST;
			
			return runLoader(request, doArtistAdded, doArtistAddError, createWrapper({ method:"library.addArtist", artist:artistName } ));
		}
		
		public function addTrack(artistName:String, trackName:String):Number
		{
			var url:String = LastFMClient.baseUrl;
			var sig:String = "api_key" + LastFMClient.apiKey + "artist" + artistName + "methodlibrary.addTracksk" + LastFMClient.sessionKey + "track" + trackName + LastFMClient.secret;
			var request:URLRequest = new URLRequest(url);
			
			var variables:URLVariables = new URLVariables();
			variables.artist = artistName;
			variables.track = trackName;
			variables.sk = LastFMClient.sessionKey;
			variables.api_sig = MD5.encrypt(sig);
			variables.method = "library.addTrack";
			variables.api_key = LastFMClient.apiKey;
			
			request.data = variables;
			request.method = URLRequestMethod.POST;
			
			var wrapper:RequestWrapper;
			
			return runLoader(request, doTrackAdded, doTrackAddError, createWrapper({ method:"library.addTrack", artist:artistName, track:trackName }) );
		}
		
		protected function doTrackAdded(evt:Event):void
		{
			var data:String = evt.target.data as String;
			var xml:XML = new XML(data);
			var wrapper:RequestWrapper = getWrapper(evt.target as URLLoader);
			
			if (xml.@status == "ok")
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.TRACK_ADDED, wrapper.requestId, wrapper.artist, "", wrapper.track));
			}
			else
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.TRACK_ADD_FAILED, wrapper.requestId, wrapper.artist, "", wrapper.track));
			}
		}
		
		protected function doTrackAddError(evt:ErrorEvent, wrapper:RequestWrapper):void
		{
			dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.TRACK_ADD_FAILED, wrapper.requestId, wrapper.artist, "", wrapper.track));
		}
		
		protected function doArtistAdded(evt:Event):void
		{
			var data:String = evt.target.data as String;
			var xml:XML = new XML(data);
			var wrapper:RequestWrapper = getWrapper(evt.target as URLLoader);
			
			if (xml.@status == "ok")
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ARTIST_ADDED, wrapper.requestId, wrapper.artist, "", ""));
			}
			else
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ARTIST_ADD_FAILED, wrapper.requestId, wrapper.artist, "", ""));
			}
		}
		
		protected function doArtistAddError(evt:ErrorEvent, wrapper:RequestWrapper):void
		{
			dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ARTIST_ADD_FAILED, wrapper.requestId, wrapper.artist, "", ""));
		}
		
		protected function doAlbumAdded(evt:Event):void
		{
			var data:String = evt.target.data as String;
			var xml:XML = new XML(data);
			var wrapper:RequestWrapper = getWrapper(evt.target as URLLoader);
			
			if (xml.@status == "ok")
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ALBUM_ADDED, wrapper.requestId, wrapper.artist, wrapper.album, ""));
			}
			else
			{
				dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ALBUM_ADD_FAILED, wrapper.requestId, wrapper.artist, wrapper.album, ""));
			}
		}
		
		protected function doAlbumAddError(evt:ErrorEvent, wrapper:RequestWrapper):void
		{
			dispatchEvent(new LFMLibraryAddEvent(LFMLibraryAddEvent.ALBUM_ADD_FAILED, wrapper.requestId, wrapper.artist, wrapper.album, ""));
		}
	}
}